package com.sam;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class LiveView extends Activity {
public static LiveView obj;
SocketService mBoundService;
boolean mIsBound = false;
ImageView image;
Bitmap bmp;
private ServiceConnection mConnection = new ServiceConnection() {
	// EDITED PART
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		// TODO Auto-generated method stub
		mBoundService = ((SocketService.LocalBinder) service).getService();
		mBoundService.runFrame = true;
		mBoundService.getTCPClient().sendMessage(new DataStream("live",null,null));

	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		// TODO Auto-generated method stub
		mBoundService = null;
	}

};

private void doBindService() {
	bindService(new Intent(this, SocketService.class), mConnection,
			Context.BIND_AUTO_CREATE);
	mIsBound = true;
	if (mBoundService != null) {
		mBoundService.IsBoundable();
	}
}

private void doUnbindService() {
	if (mIsBound) {
		// Detach our existing connection.
		unbindService(mConnection);
		mIsBound = false;
	}
}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.img_view);
		obj=this;
		doBindService();
	}
	@Override
	public void onBackPressed() {
		mBoundService.stopFrames();
		finish();
		
	}
	
	public void show(byte[] frame){
		  image = (ImageView) findViewById(R.id.imageView1);
		  image.setScaleType(ScaleType.FIT_XY); 
		  bmp = BitmapFactory.decodeByteArray(frame, 0,frame.length);
		  image.setImageBitmap(bmp);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
}
